<#assign title ="用户管理">
<#assign sexMap={"-1":"未知","0":"女","1":"男"} >
<#include "/include/layout.ftl">
<#macro css>
<link rel="stylesheet" type="text/css" href="${contextPath}/js/zTree/css/zTreeStyle/zTreeStyle.css" /> 
</#macro>
<#macro js>
</#macro>
<#macro body>
<#import "/include/select.ftl" as items>
	<section class="content-header">
      <h1>
        ${title}
      	<a class="btn btn-default pull-right back" href="javascript:;"><i class="fa fa-reply"></i> 返回</a>
      </h1>
    </section>
    <section class="invoice">
 		<div class="box box-info">
 			<div class="row">
 				 <div class="col-xs-12">
		          <h2 class="page-header">
		            <i class="fa fa-user"></i> ${data.username}
		            <small class="pull-right">状态: <#if data.status==1>启用<#else><span style="color:red">停用</span></#if></small>
		          </h2>
		        </div>
            </div>
            <div class="row">
            	<div class="col-xs-10">
			 		 <div class="table-responsive">
			            <table class="table">
			              <tr>
			                <th class="text-right">用户编号:</th>
			                <td>${data.logincode}</td>
			                <th class="text-right">手机号码:</th>
			                <td>${data.phone}</td>
			               	<th class="text-right">性别:</th>
			                <td>${sexMap[data.sex+""]}</td>
			              </tr>
			              <tr>
			                <th class="text-right">创建时间:</th>
			                <td>${long2date(data.createtime,"yyyy-MM-dd")}</td>
			                <th class="text-right">联系地址:</th>
			                <td colspan="3">${data.address}</td>
			              </tr>
			            </table>
			        </div>
			    </div>
      		</div>
      		<div class="row">
      			<div class="col-xs-10">
				     <div class="nav-tabs-custom">
			            <ul class="nav nav-tabs">
			              <li class="active"><a href="#resetpwd" data-toggle="tab">重置密码</a></li>
			              <li><a href="#user_grant" id="user_role" data-status="0" data-toggle="tab">用户授权</a></li>
			            </ul>
			            <div class="tab-content">
			            	<div class="active tab-pane" id="resetpwd">
			            		<form action="${contextPath}/sys/user/resetpwd" method="post" id="documentForm" class="form-horizontal">
			            			<div class="box-body">
				            			<div class="form-group">
						                  <label class="col-sm-5 control-label">新密码:</label>
						                  <div class="col-sm-4">
						                    <input type="password" name="password" id="pwd"  class="form-control"  placeholder="新密码" required minlength=6 maxlength=20  />
						                  </div>
						                </div>
				            			<div class="form-group">
						                  <label class="col-sm-5 control-label">确认新密码:</label>
						                  <div class="col-sm-4">
						                    <input type="password" class="form-control"  placeholder="确认新密码" equalTo="#pwd"  />
						                  </div>
						                </div>
					                </div>
					                 <div class="box-footer">
						              	<div class="col-sm-6"></div>
						              	 <div class="col-sm-6">
						              	 	<a class="btn btn-default" id="reset" ><i class="fa fa-rotate-left"></i> 重置</a>
						              	 	<a class="btn btn-info resetsubmit"><i class="fa fa-save"></i> 确定</a>
							             </div>
						              </div>
			            		</form>
			            	</div>
			            	<div class="tab-pane" id="user_grant">
			            		 <form action="${contextPath}/sys/role/save" method="post"  class="form-horizontal">
							          <input type="hidden" name="id" value="${data.id}" />
						              <div class="box-body">
						              	<div class="form-group">
						              		<div class="col-sm-5"></div>
						              		<div class="col-sm-6">
						                  		<ul id="treeMenu" class="ztree"></ul>
						                  	</div>
						                </div>
						              </div>
						               <div class="box-footer">
						              	<div class="col-sm-6"></div>
						              	 <div class="col-sm-6">
						              	 	<a class="btn btn-info grantsubmit"><i class="fa fa-save"></i> 确定</a>
							             </div>
						              </div>
						          </form>
			            	</div>
			            </div>
			          </div>
		        </div>
	        </div>
        </div>
    </section>
    <script src="${contextPath}/js/common/md5.js"></script>
    <script type="text/javascript" src="${contextPath}/js/zTree/js/jquery.ztree.core.min.js" charset="utf-8"></script>
	<script type="text/javascript" src="${contextPath}/js/zTree/js/jquery.ztree.excheck.min.js" charset="utf-8"></script>
	<script type="text/javascript">
		var setting = {
			check: {
				enable: true,
				chkDisabledInherit: true
			},
			view: {
				showIcon: false
			},
			data: {
				simpleData: {
					enable: true
				}
			}
		};
		
		$(function(){
		
			//重置密码
			$(".resetsubmit").click(function(){
				if (validateForm()) {
			        $.post("${contextPath}/sys/user/resetpwd", { "id": "${data.id}", "pwd": $("[name=password]").val().MD5() },showResponse);
					return false;
				}
			})
			
			//获取 角色树信息
			$("#user_role").click(function(){
				var status = $(this).attr("data-status");
				if(status == "0"){
					$.post("${contextPath}/sys/user/user_grant",{"userid":"${data.id}"},grantResponse);
					$(this).attr("data-status","1");
				}
			})
			
			//授权提交
			$(".grantsubmit").click(function(){
				  var treeObj=$.fn.zTree.getZTreeObj("treeMenu");
		     	  var nodes=treeObj.getCheckedNodes(true);
		     	  var ids=[];
			      for(var i=0;i<nodes.length;i++){
			           ids[i] = nodes[i].id; 
			      }
	              $.post("${contextPath}/sys/user/role_grant_save",{"userid":"${data.id}","ids":ids},showResponse);
			})
		})
		
		
		//ajax 返回
		function showResponse(responseText, statusText) {
			var code = responseText.code;
			var msg = responseText.msg;
			if (code == "1") {
				  layer.alert(msg,{icon: 1}, function(index){
					  location.reload();
				 	  layer.close(index);
				});  
			} else {
				layer.msg(msg, {icon: 2});
			}
		}	
		
		// 授权 角色树 ajax 返回
		function grantResponse(responseText, statusText) {
			var code = responseText.code;
			var msg = responseText.msg;
			if (code == "1") {
				$.fn.zTree.init($("#treeMenu"), setting, responseText.data); //建立树 
			} else {
				layer.msg(msg, {icon: 2});
			}
		}	
		
		//表单验证
		function validateForm(){
		   return $("#documentForm").validate().form();
		}
	</script>
</#macro>